﻿// -----------------------------------------------------------------------
// <copyright file="DateTimeExtensions.cs" company="The Usual Dosage">
// © 2013 by Matthew M. Martin
// </copyright>
// -----------------------------------------------------------------------

namespace Cadence.NET
{
    using System;
    using System.Globalization;

    /// <summary>
    /// Contains extensions for DateTime objects.
    /// </summary>
    public static class DateTimeExtensions
    {
        private static GregorianCalendar gregCalendar = new GregorianCalendar();

        /// <summary>
        /// Gets the week of month.
        /// </summary>
        /// <param name="time">The time.</param>
        /// <returns></returns>
        public static int GetWeekOfMonth(this DateTime time)
        {
            DateTime first = new DateTime(time.Year, time.Month, 1);
            return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
        }

        /// <summary>
        /// Gets the week of year.
        /// </summary>
        /// <param name="time">The time.</param>
        /// <returns></returns>
        public static int GetWeekOfYear(this DateTime time)
        {
            return gregCalendar.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        }

        /// <summary>
        /// Determines whether [is greater than] [the specified t].
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns>
        ///   <c>true</c> if [is greater than] [the specified t]; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsGreaterThanEqualTo(this Time t, Time compareTime)
        {
            DateTime now = DateTime.Now;
            DateTime thisDate = new DateTime(now.Year, now.Month, now.Day, t.Hour.HasValue ? t.Hour.Value : now.Hour, t.Minute.HasValue ? t.Minute.Value : now.Minute, t.Second.HasValue ? t.Second.Value : now.Second);
            DateTime compareDate = new DateTime(now.Year, now.Month, now.Day, compareTime.Hour.HasValue ? compareTime.Hour.Value : 0, compareTime.Minute.HasValue ? compareTime.Minute.Value : 0, compareTime.Second.HasValue ? compareTime.Second.Value : 0);
            
            return thisDate >= compareDate;
        }

        /// <summary>
        /// Converts a datetime to a time object
        /// </summary>
        /// <param name="t">The compareTime.</param>
        /// <returns></returns>
        public static Time ToTime(this DateTime t)
        {
            return new Time() { Hour = t.Hour, Millisecond = t.Millisecond, Minute = t.Minute, Second = t.Second };
        }
    }
}
